package com.example.vehiclefabric.controller;

import com.example.vehiclefabric.DTO.ChargingStationDTO;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.StringUtils;
import org.hyperledger.fabric.client.Contract;
import org.hyperledger.fabric.client.GatewayException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static cn.hutool.extra.spring.SpringUtil.getBean;

/**
 * @name: ChaccController
 * @description: 定义充电桩智能合约的交互方法。
 * @author: Lulu Zhao
 **/
@RestController
@RequestMapping("/chacc")
@Slf4j
@AllArgsConstructor
public class ChaccController {
    final Contract chaccContract = getBean("chaccContract", Contract.class);

    /**
     * @name: queryAllChargingStations
     * @description: 查询全部充电桩信息。
     * @params: []
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @author: Lulu Zhao
     */
    @GetMapping("/chargingstations")
    public Map<String, Object> queryAllChargingStations() throws GatewayException {
        Map<String, Object> result = Maps.newConcurrentMap();
        byte[] account = chaccContract.evaluateTransaction("QueryAllChargingStations");
        result.put("payload", StringUtils.newStringUtf8(account));
        result.put("status", "ok");
        return result;
    }

    /**
     * @name: updateVehicle
     * @description: 更新充电桩信息。
     * @params: [chargingStationDTO]
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     * @author: Lulu Zhao
     */
    @PostMapping("/update")
    public Map<String, Object> updateVehicle(ChargingStationDTO chargingStationDTO) throws GatewayException {

        Map<String, Object> result = Maps.newConcurrentMap();

        chaccContract.newProposal("UpdateVehicleState")
                .addArguments(chargingStationDTO.getChargingStationID(), chargingStationDTO.getLocation(), chargingStationDTO.getBrand(), chargingStationDTO.getProvider(), chargingStationDTO.getState())
                .build()
                .endorse()
                .submitAsync();

        result.put("status", "ok");

        return result;
    }

    /**
     * @name: queryChargingStationById
     * @description: 根据ID查询充电桩信息。
     * @params: [chargingStationId]
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     * @author: Lulu Zhao
     */
    @GetMapping("/queryChargingStationById")
    public Map<String, Object> queryChargingStationById(String chargingStationId) throws GatewayException {

        Map<String, Object> result = Maps.newConcurrentMap();

        byte[] account = chaccContract.evaluateTransaction("QueryChargingStation", chargingStationId);

        result.put("payload", StringUtils.newStringUtf8(account));

        result.put("status", "ok");

        return result;
    }
}
